#include <bits/stdc++.h>
using namespace std;

long long a[200005],firstne[200005],cnt[1000005],f[200005],Near[200005];

int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t,n;
    cin >> t;
    for(int i = 0;i < t;i++) {
        cin >> n;
        for(int j = 0;j < 1000005;j++) cnt[j] = -1;
        for(int j = 0;j < n;j++) {
            f[j] = 1000000000000;
            cin >> a[j];
            if(cnt[a[j]] == -1) {
                firstne[j] = -1;
                cnt[a[j]] = j;
            }
            else {
                firstne[j] = cnt[a[j]];
                cnt[a[j]] = j;
            }
            if(j > 0 && a[j] == a[j - 1]) {
                Near[j] = a[j];
            }
            else Near[j] = 0;
        }
        for(int j = 1;j < n;j++) {
            Near[j] += Near[j - 1];
        }
        for(int j = 0;j < n;j++) {
            if(firstne[j] == -1) {
                if(j > 0) f[j] = f[j - 1];
                else f[j] = 0;
            }
            else {
                if(a[j] == a[j - 1]) f[j] = f[j - 1] + a[j];
                else {
                    f[j] = max(f[j - 1],Near[j - 1] - Near[firstne[j] + 1] + f[firstne[j] + 1] + a[j]);
                }
            }
        }
        cout << f[n - 1] << endl;
    }
    return 0;
}
